package cn.yunhe.service;

import cn.yunhe.dao.IDistanceDao;
import cn.yunhe.entity.Distance;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.awt.geom.Point2D;


import static cn.yunhe.util.BaiduUtil.getDistance;

public class IDistanceService {
    SqlSession sqlSession = null;
    IDistanceDao distanceDao = null;

    public double getDistanceTo(String name1,String name2){
        sqlSession = SqlSessionFactoryUtil.getSqlSession();
        distanceDao = sqlSession.getMapper(IDistanceDao.class);
        Distance distance =distanceDao.getDistanceTo(name1);
        Distance distance1 = distanceDao.getDistanceTo(name2);
        Point2D pointDD = new Point2D.Double(distance.getJingdu(), distance.getWeidu());

        Point2D pointXD = new Point2D.Double(distance1.getJingdu(), distance1.getWeidu());

        double gongli =getDistance(pointDD, pointXD)/1000;

        sqlSession.close();
        return gongli;
    }


}
